//
//  AppSecurityInfo.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/26.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AppSecurityInfo: NSObject {
    
    static let `default` = AppSecurityInfo()
    var codeType = 0
    var model:SecurityModel = SecurityModel()
    struct SecurityInfo {
        static let screenLock =  "screenLock"
        static let openScreenKey = "openScreenKey"
        static let screenPassword = "screenPassword"
    }
    var passWordDic: [String:String] = [:]
    
   
    var successBlock:success?
    var failBlock:fail?
    
    lazy var request: SecurityRequest = {
        let api = SecurityRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var codeRequest:MobileCodeRequest = {
        let api = MobileCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var emailRequest:EmailCodeRequest = {
        let api = EmailCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    /// 获取验证信息
    ///
    /// - Parameters:
    ///   - success: 成功回调
    ///   - fail: 失败回调
    func archiveSecurityInfo(success:success? = nil,fail:fail? = nil) {
        self.successBlock = success
        self.failBlock = fail
        self.request.loadData()
    }
    
    func  archiveMobileCode(success:success? = nil,fail:fail? = nil)  {
        self.successBlock = success
        self.failBlock = fail
        self.codeRequest.loadData()
    }
    
    func openScreenLock(dic:[String:String]) {
        passWordDic = dic
        UserDefaults.standard.set(dic, forKey: SecurityInfo.screenLock)
    }
    
    func readDic() {
        if (UserDefaults.standard.object(forKey: SecurityInfo.screenLock) as? [String:String]) != nil {
           passWordDic =  (UserDefaults.standard.object(forKey: SecurityInfo.screenLock)) as! [String:String]
        }else {
        passWordDic = [:]
        }
    }

}
extension AppSecurityInfo: RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if ((data?["data"] as? [String:Any]) != nil) {
            let model = SecurityModel(JSON: data!["data"] as! [String:Any])
            if model != nil {
                self.model = model!
            }
        }
        if self.successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if self.failBlock != nil {
            if ((request as? MobileCodeRequest) != nil) || ((request as? EmailCodeRequest) != nil) {
                CYMBShow.showMessage(request.errorMsg)
            }
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? MobileCodeRequest {
            pramas.updateValue(model.areaCode.ch_length != 0 ? model.areaCode as Any : "" , forKey: "areaCode")
            pramas.updateValue(model.mobile.ch_length != 0 ? model.mobile as Any : "", forKey: "mobile")
            pramas.updateValue(codeType, forKey: "codeType")
            pramas.updateValue(0, forKey: "type")
        }else if let _ = request as? EmailCodeRequest {
            pramas.updateValue(codeType, forKey: "codeType")
            pramas.updateValue(model.areaCode.ch_length != 0 ? model.areaCode as Any : "" , forKey: "areaCode")
            pramas.updateValue(model.email.ch_length != 0 ? model.email as Any : "", forKey: "email")
            pramas.updateValue(1, forKey: "type")
        }
        
        return pramas
    }
    
    
    
}
